package com.bey.keepgoing.service.account;

import org.springframework.beans.factory.annotation.Autowired;

import com.bey.keepgoing.model.User;
import com.bey.keepgoing.repository.dao.AccountDao;
import com.bey.keepgoing.utils.DateProvider;
import com.bey.keepgoing.utils.Encodes;
import com.bey.keepgoing.utils.security.Digests;

public class AccountContext {

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	private DateProvider dateProvider = DateProvider.DEFAULT;

	private AccountDao accountDao;

	public void registerUser(User user) {
		entryptPassword(user);
		user.setRoles("user");
		user.setRegisterDate(dateProvider.getDate());

		accountDao.save(user);
	}

	private void entryptPassword(User user) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		user.setSalt(Encodes.encodeHex(salt));
		byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(),
				salt, HASH_INTERATIONS);
		user.setPassword(Encodes.encodeHex(hashPassword));
	}

	@Autowired
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

}
